package com.gistmap.ntdc.persistence.domain;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author zhangran
 * @date 2018/12/19
 */
@Getter
@Setter
@Entity
@Table(name = "user")
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Column(unique = true)
    private String openId;

    @Column(unique = true)
    private String token;

    private String nickName;

    private String gender;

    private String language;

    private String city;

    private String province;

    private String country;

    private String avatarUrl;

    private String unionId;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<Address> addresses;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<MerchantOrder> merchantOrders;


    /**
     * 上一次登陆时间
     */
    @LastModifiedDate
    private Date updatedTime;

    @CreatedDate
    private Date createdTime;

    public User(WxMaUserInfo userInfo){
        if (userInfo != null) {
            BeanUtils.copyProperties(userInfo, this);
        }
    }
}
